#region Using Statements
using System;
using System.Collections.Generic;
using System.Windows.Forms;
//using Raytrace;
using TNI;

using System.Threading;
using TNI.Operations;
using XMLConverter;
#endregion

namespace NI
{

    public class NIform : Form
    {

        private NIController controller;

        private MenuStrip menuStrip1;
        private ToolStripMenuItem fichierToolStripMenuItem;
        private ToolStripMenuItem ouvrirImageToolStripMenuItem;
        private ToolStripMenuItem sauvegarderImageToolStripMenuItem;
        private ToolStripMenuItem traitementToolStripMenuItem;
        private ToolStripMenuItem renduToolStripMenuItem;
        private SplitContainer splitContainer1;
        private ProgressBar progressBar1;
        private PictureBox pictureBox1;
        private ToolStripMenuItem lancerDeRayonToolStripMenuItem;
        private ToolStripMenuItem gradiantToolStripMenuItem;
        private ToolStripMenuItem ouvrirSceneToolStripMenuItem;
        private ImageTNI image;
    
        public NIform()
   
        {
            this.controller = new NIController();
            this.controller.raytraceEnvent +=new NIController.RayTraceEventHandler(controller_raytraceEnvent);
            InitializeComponent();
            
            //this.Show();
        }

        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fichierToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.ouvrirImageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.sauvegarderImageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.ouvrirSceneToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.traitementToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.gradiantToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.renduToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.lancerDeRayonToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.menuStrip1.SuspendLayout();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fichierToolStripMenuItem,
            this.traitementToolStripMenuItem,
            this.renduToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(420, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fichierToolStripMenuItem
            // 
            this.fichierToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.ouvrirImageToolStripMenuItem,
            this.sauvegarderImageToolStripMenuItem,
            this.ouvrirSceneToolStripMenuItem});
            this.fichierToolStripMenuItem.Name = "fichierToolStripMenuItem";
            this.fichierToolStripMenuItem.Size = new System.Drawing.Size(54, 20);
            this.fichierToolStripMenuItem.Text = "Fichier";
            // 
            // ouvrirImageToolStripMenuItem
            // 
            this.ouvrirImageToolStripMenuItem.Name = "ouvrirImageToolStripMenuItem";
            this.ouvrirImageToolStripMenuItem.Size = new System.Drawing.Size(183, 22);
            this.ouvrirImageToolStripMenuItem.Text = "Ouvrir image";
            this.ouvrirImageToolStripMenuItem.Click += new System.EventHandler(this.ouvrirImageToolStripMenuItem_Click);
            // 
            // sauvegarderImageToolStripMenuItem
            // 
            this.sauvegarderImageToolStripMenuItem.Name = "sauvegarderImageToolStripMenuItem";
            this.sauvegarderImageToolStripMenuItem.Size = new System.Drawing.Size(183, 22);
            this.sauvegarderImageToolStripMenuItem.Text = "Sauvegarder image";
            this.sauvegarderImageToolStripMenuItem.Click += new System.EventHandler(this.sauvegarderImageToolStripMenuItem_Click);
            // 
            // ouvrirSceneToolStripMenuItem
            // 
            this.ouvrirSceneToolStripMenuItem.Name = "ouvrirSceneToolStripMenuItem";
            this.ouvrirSceneToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O)));
            this.ouvrirSceneToolStripMenuItem.Size = new System.Drawing.Size(183, 22);
            this.ouvrirSceneToolStripMenuItem.Text = "Ouvrir scene";
            this.ouvrirSceneToolStripMenuItem.Click += new System.EventHandler(this.ouvrirSceneToolStripMenuItem_Click);
            // 
            // traitementToolStripMenuItem
            // 
            this.traitementToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.gradiantToolStripMenuItem});
            this.traitementToolStripMenuItem.Name = "traitementToolStripMenuItem";
            this.traitementToolStripMenuItem.Size = new System.Drawing.Size(77, 20);
            this.traitementToolStripMenuItem.Text = "Traitement";
            // 
            // gradiantToolStripMenuItem
            // 
            this.gradiantToolStripMenuItem.Name = "gradiantToolStripMenuItem";
            this.gradiantToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.gradiantToolStripMenuItem.Text = "Gradiant";
            this.gradiantToolStripMenuItem.Click += new System.EventHandler(this.gradiantToolStripMenuItem_Click);
            // 
            // renduToolStripMenuItem
            // 
            this.renduToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.lancerDeRayonToolStripMenuItem});
            this.renduToolStripMenuItem.Name = "renduToolStripMenuItem";
            this.renduToolStripMenuItem.Size = new System.Drawing.Size(53, 20);
            this.renduToolStripMenuItem.Text = "Rendu";
            // 
            // lancerDeRayonToolStripMenuItem
            // 
            this.lancerDeRayonToolStripMenuItem.Name = "lancerDeRayonToolStripMenuItem";
            this.lancerDeRayonToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.R)));
            this.lancerDeRayonToolStripMenuItem.Size = new System.Drawing.Size(199, 22);
            this.lancerDeRayonToolStripMenuItem.Text = "Lancer de rayon";
            this.lancerDeRayonToolStripMenuItem.Click += new System.EventHandler(this.lancerDeRayonToolStripMenuItem_Click);
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
            this.splitContainer1.Location = new System.Drawing.Point(0, 24);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.pictureBox1);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.progressBar1);
            this.splitContainer1.Size = new System.Drawing.Size(420, 378);
            this.splitContainer1.SplitterDistance = 349;
            this.splitContainer1.TabIndex = 1;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(420, 349);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // progressBar1
            // 
            this.progressBar1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.progressBar1.Location = new System.Drawing.Point(0, 0);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(420, 25);
            this.progressBar1.TabIndex = 0;
            // 
            // NIform
            // 
            this.ClientSize = new System.Drawing.Size(420, 402);
            this.Controls.Add(this.splitContainer1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "NIform";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void lancerDeRayonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //RaytraceProperties properties = new RaytraceProperties();
            //properties.SetController(this.controller);
            //properties.Show();

            if (this.controller != null)
            {
                this.controller.RaiseRaytrace();
            }
        }

        private void controller_raytraceEnvent()
        {
            this.pictureBox1.Image = this.controller.Render(ref progressBar1);
        }

        private void ouvrirImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.ShowDialog();
            if (fd.FileName != string.Empty)
            {
                this.image = new ImageTNI(fd.FileName);

                this.pictureBox1.Image = image.GetBitmapImage();
            }
        }

        private void sauvegarderImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog fd = new SaveFileDialog();
            fd.ShowDialog();
            if (fd.FileName != string.Empty)
            {

                this.pictureBox1.Image.Save(fd.FileName);
            }
        }

        

        private void gradiantToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            
            Console.Write("Gradiant ... ");

            

            GradiantMorphologique gradiant = new GradiantMorphologique();

            Voisinage voisinage = new Voisinage();

            Signal2D<TNI.Color> signal2d = new Signal2D<TNI.Color>();
            signal2d.Data = image.Data;
            signal2d.NombreDeColonne = image.NombreDeColonne;
            signal2d.NombreDeLigne = image.NombreDeLigne;

            gradiant.Operate(ref signal2d, ref voisinage);

            
            image.Data = signal2d.Data;

            if (image != null)
            {
                this.pictureBox1.Image = image.GetGrayscaleNaturalImage();
            }

            Console.WriteLine("[OK]");
        }

        private void ouvrirSceneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            f.ShowDialog();
            if (f.FileName != "")
            {
                XMLGeometryConverter converter = new XMLGeometryConverter();
                List<Graphics.Object3D> objs = converter.ParseGeometries(f.FileName);
                List<Graphics.Light> lights = converter.ParseLights(f.FileName);
                Graphics.Camera camera = converter.ParseCamera(f.FileName);
                Graphics.Film film = converter.ParseFilm(f.FileName);
                controller.Objects = objs;
                controller.Lights = lights;
                controller.SetFilm(film);
                controller.SetCamera(camera);
            }

        }

       

    }
}


